package com.janal.service.login;

import com.janal.model.user.User;
import com.janal.model.user.UserDao;
import com.janal.model.user.UserDaoImpl;
import com.janal.util.JdbcUtils;

import java.sql.Connection;
import java.sql.SQLException;

public class LoginServiceImpl implements LoginService{
    private UserDao userDao;

    public LoginServiceImpl() {
        userDao = new UserDaoImpl();
    }

    @Override
    public User login(User user) {
        Connection conn = null;
        User resultuser = null;
        try {
            conn = JdbcUtils.getConnection();
            resultuser = userDao.getLoginUser(conn,user.getUsercode());
            //若resultuser为空，说明用户不存在
            if(resultuser != null){
                if(!resultuser.getPassword().equals(user.getPassword())){
                    //密码不正确，置resultuser的密码为空
                    resultuser.setPassword(null);
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JdbcUtils.close(null,conn);
        }
        return resultuser;
    }
}
